<?php
namespace App\Http\Controllers\Console;
use App\Http\Controllers\Controller;
use App\Model\ShopModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use App\Service\RedisService;
use phpDocumentor\Reflection\DocBlock\Tags\Var_;


class RoleController extends Controller
{

    /**添加编辑角色
        id：角色id,编辑时传递此参。
        name:角色名称
     * @param Request $request
     * @return array
     */
    public function act_role(Request $request){

        $name = $request->input('name')? $request->input('name'):'';
        if($name == ""){
            return ["code"=>0,"msg"=>"保存失败，请检查数据格式是否正确"];
        }

        $data = [ 'name' => $name ];
        if($request->input("id")) {

            $mid = DB::table("admin_role")->where("id", $request->input("id"))->update($data);
        }else {
            $mid = DB::table("admin_role")->insertGetId($data);
        }

        if($mid>0)
        {
            $arr=["code"=>200,"msg"=>"保存成功"];
        }else
            $arr=["code"=>0,"msg"=>"保存失败，请检查数据格式是否正确"];

        return $arr;

    }

    /**获取角色列表
     * @param Request $request
     * @return array
     */
    public function role_lists(Request $request){

        $role = DB::select('select r.id,r.name,group_concat(p.name) as purview from admin_role as r left join admin_role_purview as rp on r.id = rp.admin_role_id left join admin_purview as p on rp.admin_purview_id = p.id group by r.name,r.id');

        if($role){
            $arr=["code"=>200,"msg"=>"获取数据成功","data"=>$role];
        }else
            $arr=["code"=>0,"msg"=>"获取数据失败"];

        return $arr;
    }


    /**删除角色
     * @param Request $request
     * @return array
     */
    public function del_role(Request $request){

        $id=$request->input("id");
        if($id==""){
            $arr=["code"=>0,"msg"=>"id不能为空"];
            return $arr;
        }
        //判断角色是否还在使用中
        $role_id=DB::table('admin')->where('admin_role_id','=',$id)->first();
        if($role_id){
            $arr=["code"=>0,"msg"=>"角色不允许删除"];
            return $arr;
        }
        $re=DB::table('admin_role')->where("id",$id)->delete();
        if($re>0){
            $arr=["code"=>200,"msg"=>"删除成功"];
        } else
            $arr=["code"=>0,"msg"=>"删除失败,请检查数据格式"];

        return $arr;
    }
    /**分配角色权限
     * @param Request $request
     * @return array
     */
    public function act_role_purview(Request $request){

        $id = $request->input('id')? $request->input('id'):'';
        //权限接收
        $purview = $request->input('purview')?$request->input('purview'):'';

        if($id == "" || $purview == ""){
            return ["code"=>0,"msg"=>"请检查数据格式是否正确"];
        }
        //查找角色是否存在
        $role_res = DB::table('admin_role')->select('id')->where('id','=',$id)->first();
        if(!$role_res){
            return ["code"=>1002,"msg"=>"角色不存在，请检查数据格式"];
        }
        //删除原有权限
        $re = DB::table('admin_role_purview')->where('admin_role_id',$id)->delete();
        //循环添加新的权限
        foreach ($purview as $k => $v){

            $mid = DB::table("admin_role_purview")->insertGetId(['admin_role_id'=>$id,'admin_purview_id'=>$v]);
            if(!$mid) break;
        }

        if($mid)
        {
            $arr=["code"=>200,"msg"=>"保存成功"];
        }else
            $arr=["code"=>0,"msg"=>"保存失败，请检查数据格式是否正确"];

        return $arr;

    }
    /**
     * 权限下拉菜单
     * @return array
     */
    public function purviews(Request $request){

        $role_id = $request->input('role_id')?$request->input('role_id'):0;
        if(!$role_id){
            return ["code"=>1003,"msg"=>"请检查数据格式是否正确"];
        }

        //获取该角色权限
        $role_purview = DB::table('admin_role_purview')->select('admin_purview_id')->where('admin_role_id','=',$role_id)->get()->toArray();
        if(!$role_purview){
            return ["code"=>1002,"msg"=>"出错，角色不存在"];
        }
        $role_purview = array_column($role_purview, 'admin_purview_id');

        $p_p = DB::table('admin_purview')
            ->select('id','name')->where('parent_id','=', 0)->orderBy('id')->get()->toArray();
        if($p_p){
            //获取子集权限
            $data = [];
            foreach ($p_p as $k => $item) {
                //判断权限是否被选择
                $item->check = 0;
                if(in_array($item->id,$role_purview)){
                    $item->check = 1;
                }
                $s_p = DB::table('admin_purview')->select('id','name')->where('parent_id','=',$item->id)->get()->toArray();
                if($s_p){
                    //循环判断权限是否被选择
                    foreach($s_p as $k => $s_item){
                        $s_item->check = 0;
                        if(in_array($s_item->id,$role_purview)){
                            $s_item->check = 1;
                        }
                    }
                }
                $item->children = $s_p;
                $data[]=$item;
            }
            if(!$data){
                return ['code'=>1001,'msg'=>'获取数据失败'];
            }
        }else{
            return ['code'=>1001,'msg'=>'获取数据失败'];
        }
        return ['code'=>200,'msg'=>'获取数据成功','data'=>$data];

    }


}